<template>
  <div class="bank-page" v-loading="loading" element-loading-text="加载中...">
    <div class="bank-title">
      <div class="bank-title-left">
        银行管理
      </div>
      <div class="bank-title-right">

        <el-button type="primary" :icon="Plus" @click="addDeptFun('add')">
          添加
        </el-button>
      </div>
    </div>
    <div class="bank-table">
      <el-table border stripe :data="tableData" :height="store.tableHeight">
        <el-table-column prop="bank_wseq" label="银行编号" />
        <el-table-column prop="cover" label="银行LOGO" width="100" align="center">
          <template #default="{ row }">

            <el-image :preview-teleported="true" style="width: 50px; height: 50px" :src="imgSrc(row.bank_logo)"
              :zoom-rate="1.2" :preview-src-list="[imgSrc(row.bank_logo)]" :initial-index="4" fit="cover" />
          </template>
        </el-table-column>

        <el-table-column prop="bank_name" label="银行名称" />
        <el-table-column label="背景色" align="center">
          <template #default="{ row }">
            <div :style="{ width: '100%', height: '100%', background: row.bank_bg }">
              {{ row.bank_bg }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" width="90">
          <template #default="{ row }">
            <el-switch v-model="row.status" active-text="启用" :active-value="1" inactive-text="禁用" :inactive-value="0"
              inline-prompt @change="statusFun(row)" />
          </template>
        </el-table-column>

        <el-table-column label="操作" width="120" align="center">
          <template #default="{ row }">
            <el-button type="primary" link @click="updateDeptFun('update', row)">
              编辑
            </el-button>
            <el-button type="danger" link @click="delFun(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
  <addDialog :treeData="tableData" ref="addDialogRef" @refreshList="refreshList" />
  <imgDialog ref="imgDialogRef" />
</template>
<script setup>
import { Plus } from "@element-plus/icons-vue";
import { useCounterStore } from "@/stores/counter.js";
import { listApi, delApi, stateApi } from "@/plugins/api/system/bank";
import addDialog from "./components/DialogBank.vue";
const store = useCounterStore();
let searchForm = ref({});
function searchFun() {
  selectData.value = [];
  tableData.value = [];
  getData();
}
function resetFun() {
  searchForm.value = {};
  tableData.value = [];
  selectData.value = [];
  getData();
}
let tableData = ref([]);
let loading = ref(false);
function getData() {
  loading.value = true;
  listApi(searchForm.value)
    .then((data) => {
      tableData.value = data;
      loading.value = false;
    })
    .catch(() => {
      loading.value = false;
    });
}
let addDialogRef = ref(null);
function addDeptFun(type) {
  addDialogRef.value.initFun(type);
}
function updateDeptFun(type, row) {
  addDialogRef.value.initFun(type, row);
}
function refreshList() {
  getData();
}
let selectData = ref([]);
function handleSelectionChange(val) {
  selectData.value = val;
}
function delFun(row) {
  ElMessageBox.confirm("确认删除，是否继续?", "提示", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      delApi({
        id: row.id.toString(),
      }).then(() => {
        ElMessage({
          type: "success",
          message: "删除成功！",
        });
        getData();
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "已取消。",
      });
    });
}
function imgSrc(path) {
  return import.meta.env.VITE_APP_BASE_API + path;
}


function statusFun(row) {
  stateApi({
    status: row.status,
    id: row.id,
  }).then(() => {
    ElMessage({
      type: "success",
      message: "修改成功！",
    });
    getData();
  });
}
// 查看图片
let imgDialogRef = ref(null);
function lookImg(src) {
  imgDialogRef.value.initFun(src);
}
onMounted(() => {
  getData();
});
</script>
<style scoped lang="scss">
.bank-page {
  width: 100%;
  height: 100%;

  .bank-title {
    width: 100%;
    display: flex;
    justify-content: space-between;
    border-bottom: 1px solid #eeeeee;
    padding-bottom: 10px;

    .bank-title-left {
      font-size: 18px;
      font-weight: 700;
      color: #172b4d;
      align-self: center;

      :deep(.el-form-item) {
        margin-bottom: 0;
      }
    }
  }

  .bank-table {
    padding: 10px;
    box-sizing: border-box;

    .table-img {
      width: 50px;
      height: 50px;
      object-fit: cover;
      cursor: pointer;

      &:hover {
        transform: scale(0.97);
        transition: all 0.3s;
      }
    }
  }
}
</style>
